package com.tim.app.ui.adapter.search;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.tim.app.R;
import com.tim.app.server.entry.Article;
import com.tim.app.util.TextUtil;

import java.util.List;


/**
 * 文章
 */
public class SearchArticleAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Article> {

    private Context mContext;

    private String keyWord = "";

    public SearchArticleAdapter(Context mContext, List<Article> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_search_article, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, Article data) {
        if (data == null) {
            return;
        }
        ViewHolder holder = (ViewHolder) mHolder;

        if (!TextUtils.isEmpty(data.getTitle())) {
            TextUtil.setTextHighLight(holder.tvArticleTitle, getKeyWord(), data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            TextUtil.setTextHighLight(holder.tvArticleContent, getKeyWord(), data.getContent());
        }
        holder.tvPriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.tvCommentNumber, String.valueOf(data.getCommentCount())));
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        TextView tvArticleTitle, tvArticleContent, tvPriseNumber, tvCommentNumber;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            tvArticleTitle = (TextView) itemView.findViewById(R.id.tvArticleTitle);
            tvArticleContent = (TextView) itemView.findViewById(R.id.tvArticleContent);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
